/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.jome.ctrl.om;

/**
 *
 * @author Mr.Sam
 */
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import fr.inria.openmath.omapi.*;
import fr.inria.openmath.omapi.implementation.*;
import fr.ove.openmath.jome.ctrl.linear.events.*;
import fr.ove.openmath.jome.ctrl.om.*;

/**
* A parser for OpenMath objects.
*
* @author © 1999 DIRAT Laurent
* @version 1.0 06/10/1999
*/
public class OMParser implements Serializable, LinearParserEventSource {
    OMTreeParserHandler parserHandler;
    Parser parser;
    
    /**
    * The list of listeners of the instance
    */
    private Vector listeners = new Vector();

    
    public OMParser() {
        parserHandler = new OMTreeParserHandler();
        parser = new XMLParser(parserHandler);
        parserHandler.setEventSource(this);
    }
    
    public void parseOMObject(String omObject) {
	    ByteArrayInputStream in = new ByteArrayInputStream(omObject.getBytes());
   
        try {
            parser.initParse(in);
            // Setup done, now call the parser main loop.
            parser.parseObject(in);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void initParse(InputStream is) {
        parser.initParse(is);
    }
    
    public void parseObject(InputStream is) {
        parser.parseObject(is);
    }
    
    /**
    * Registers another listener of the instance.
    * @param linearParserListener the listener to add.
    */
    public synchronized void addLinearParserListener(LinearParserListener linearParserListener) {
        listeners.addElement(linearParserListener);
    }

    /**
    * Removes a listener.
    * @param linearParserListener the listener to remove.
    */
    public synchronized void removeLinearParserListener(LinearParserListener linearParserListener) {
        listeners.removeElement(linearParserListener);
    }

    /**
    * Fires a LinearParserEvent event to registered listeners.
    * @param linearParserEvent event encapsulating relevant information.
    */
    public void fireLinearParserEvent(LinearParserEvent linearParserEvent) {
        for (int i = 0; i < listeners.size(); i++)
          ((LinearParserListener) listeners.elementAt(i)).consumeLinearParserEvent(linearParserEvent);
    }
}